<?php

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Log;
use think\Db;
use app\common\library\Config;
use think\helper\Time;

class ProjectWorkreg extends Command
{


    //命令描述
    protected function configure()
    {
        $this->setName('ProjectWorkreg')->setDescription('work time run and wx');
    }

    //所要执行的命令
    protected function execute(Input $input, Output $output)
    {
        //开始发送
        Config::config();
        $ulist = DB::name('product_run')
            ->where('status', 1)
            ->where('product_id', 10)
            ->where('e_time', '>=', time())
            ->cache(43200)//约12小时
            ->select();
        $output->writeln("workreg  run ");
        $this->workreg($ulist);
        $this->delqrcode();

    }
    function delqrcode(){
        array_map('unlink',glob('public/qrcode/*.png'));
    }
    function dbconfig($id)
    {
        return [
            'type' => 'mysql',
            // 服务器地址
            'hostname' => 'rm-2zea426o72962o97z125010.mysql.rds.aliyuncs.com',
            // 数据库名
            'database' => 'u' . $id,
            // 用户名
            'username' => 'weibuyun',
            // 密码
            'password' => 'Wei@#$1209C!=',
        ];
    }
    protected function workreg($ulist)
    {
        list($s_time, $e_time) = Time::today();
        //取出所有员工
        foreach ($ulist as $info2) {
            $list = DB::connect($this->dbconfig($info2['id']))->view('auth_group_access', 'uid')
                ->view('admin', 'nickname', 'auth_group_access.uid=admin.id')
                ->where('status', 1)
                ->where('group_id', 11)
                ->distinct(true)
                ->select();

            //查看今天是不是周六周日
            $data = [];
            //查看今天是不是节假日
            $festival = DB::connect($this->dbconfig($info2['id']))->name('festival')->where('d_time', $s_time)->find();
            if ($festival) {
                $status = 3;
            } else {
                $status = 1;
                $festival['info'] = '';
            }
            //建立表
            foreach ($list as $info) {
                $item = [];
                $item['uid'] = $info['uid'];
                $item['d_time'] = $s_time;
                if (DB::connect($this->dbconfig($info2['id']))->name('workreg_d')->where($item)->find()) {
                    //存在请假情况或已经有记录

                } else {
                    $item['info'] = $festival['info'];
                    $item['status'] = $status;
                    $data[] = $item;
                }
            }

            DB::connect($this->dbconfig($info2['id']))->name('workreg_d')->insertAll($data);

        }
    }


}